import time
import unittest
from quto_auto.base.browseroperation import BrowserOperation
from quto_auto.base.browseroperationex import BrowserOperationEx
from quto_auto.base.usebrowser import UseBrowser
from quto_auto.util.exloperation import ExlOpera
from quto_auto.util.yamloperation import YamlOperation


class CustomerPage:

    def __init__(self):
        self.bo = BrowserOperation(UseBrowser.driver)
        self.boex = BrowserOperationEx(UseBrowser.driver)
        self.yo=YamlOperation()
        self.exl = ExlOpera()
        self.exl.get_sheet('客户用例参数')

    #点击客户管理后，切回右边框架
    def customemanagement(self):
        self.boex.change_frame(self.yo.get_locator('CustomerPage','frame_links'))
        self.bo.click('xpath',self.yo.get_locator('CustomerPage','customer_xpath'))  # 客户管理
        self.boex.change_frame(self.yo.get_locator('CustomerPage','frame_main'))

    #添加功能
    def addcustomer(self,customerNO,customerName='',phone='',address='',relationman=''):
        self.bo.click('link_text', '新增客户信息')
        self.boex.window_hand('新增客户信息')
        self.bo.input_text('name',self.yo.get_locator('CustomerPage','customerNO'), customerNO)
        self.bo.input_text('name', self.yo.get_locator('CustomerPage','customerName'), customerName)
        self.bo.input_text('name', self.yo.get_locator('CustomerPage','phone'), phone)
        self.bo.input_text('name', self.yo.get_locator('CustomerPage','address'), address)
        self.bo.input_text('name', self.yo.get_locator('CustomerPage','relationman'), relationman)
        self.bo.click('name',self.yo.get_locator('CustomerPage','saveButton'))

    # 获取添加客户成功的信息文本
    def add_sucess_info(self):
        return self.bo.get_text('xpath',self.yo.get_locator('CustomerPage','addsuc_text_xpath'))

    #正确修改，客户姓名
    def modifycustomer(self,customerN0,customerName):
        #获取页数
        ys=self.bo.search_pieces('xpath',self.yo.get_locator('CustomerPage','lst_pieces_xapth'))
        self.bo.click_for_info(ys,customerN0,2) #通过编号进行修改
        self.boex.window_hand('更新客户信息')
        self.bo.input_text('name', self.yo.get_locator('CustomerPage','customerName'), customerName)
        self.bo.click('name',self.yo.get_locator('CustomerPage','saveButton'))

    #返回修改成功的信息文本
    def modifycustomer_info(self):
        return self.bo.get_text('xpath',self.yo.get_locator('CustomerPage','modfsuc_xapth'))

    #正确删除客户
    def delcustomer(self,customerN0):
        ys = self.bo.search_pieces('xpath', self.yo.get_locator('CustomerPage', 'lst_pieces_xapth'))
        self.bo.click_for_info(ys,customerN0,1)   # 通过编号进行删除
        #对警告窗操作，1确定，2取消
        self.bo.alert_opertaion(1)

    #查询客户
    def mangerselect(self,customerN0='', customerName='',phone='',address='',relationman=''):
        self.boex.change_frame(self.yo.get_locator('CustomerPage', 'frame_links'))
        self.bo.click('xpath', self.yo.get_locator('CustomerPage', 'select_cutomer_xapth'))  # 查询客户
        self.boex.change_frame(self.yo.get_locator('CustomerPage', 'frame_main'))
        #输入内容
        self.bo.input_text('name', self.yo.get_locator('CustomerPage', 'customerNO'),customerN0)
        self.bo.input_text('name', self.yo.get_locator('CustomerPage', 'customerName'), customerName)
        self.bo.input_text('name', self.yo.get_locator('CustomerPage', 'phone'), phone)
        self.bo.input_text('name', self.yo.get_locator('CustomerPage', 'address'), address)
        self.bo.input_text('name', self.yo.get_locator('CustomerPage', 'relationman'), relationman)
        self.bo.click('name', self.yo.get_locator('CustomerPage', 'saveButton'))  # 点击查询

    #获取一条查询结果的数据信息,取1-6列的内容
    def get_one_manger_info(self,star_td=1,end_td=7):
        cus_lst=[]
        for i in range(star_td,end_td):
            text=self.bo.get_text('xpath', '/html/body/center/form'
                                      '/table[1]/tbody/tr[2]/td[' + str(i) + ']')
            cus_lst.append(text)
            cus_lst.sort()  #排序
        return cus_lst

